
package JET.SA;

import JET.Centers.ControlCenter;
import JET.Frames.DataModels.DataModelsFactory;
import JET.Scene;
import JET.SceneHelper;
import JET.SceneItem;
import JET.physics.Spring;
import JET.physics.bodies.SpringBody;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author 057Ry
 */
public class SA_Spring {

    private static ControlCenter CC = ControlCenter.getInstance();

    ////////////////////////////////////////////////////////////////////////////
    //                           S P R I N G                                  //
    ////////////////////////////////////////////////////////////////////////////

    public static void addSpringToSpringBody(Spring spring, SpringBody sb, boolean updateSceneTreeSelection) {
        sb.addSpring(spring);
        if (updateSceneTreeSelection)
            DataModelsFactory.getSceneTreeModel().fireSpringBodyChanged(sb);
    }

    public static void removeSpringFromSpringBody(Spring spring, Scene scene, boolean updateSceneTreeSelection) {
        SpringBody sb = SceneHelper.getSpringBodyThatOwnSpring(scene, spring);

        if (sb != null) {
            SA_Selection.removeSpringFromSelection(spring, scene, true);
            sb.removeSpring(spring);
            if (updateSceneTreeSelection)
                DataModelsFactory.getSceneTreeModel().fireSpringBodyChanged(sb);
        }
    }

    public static void addSpringToSceneItem(Spring spring, SceneItem si, boolean updateSceneItemTreeSelection) {
        si.getEngine().addSpring(spring);

        if (updateSceneItemTreeSelection) {
            // Do sth with selection ...
        }
    }

    public static void removeSpringFromSceneItem(Spring spring, Scene scene, boolean updateSceneTreeSelection) {
        SceneItem si = SceneHelper.getSceneItemThatOwnSpring(scene, spring);

        if (si != null) {
            SA_Selection.removeSpringFromSelection(spring, scene, true);
            si.getEngine().springList.remove(spring);
//            if (updateSceneTreeSelection)
//                DataModelsFactory.getSceneTreeModel().fireSpringBodyChanged(sb);
        }
    }

    public static void removeSpringsFromSpringBody(ArrayList<Spring> springs, Scene scene, boolean updateSceneTreeSelection) {
        for(Spring s : springs)
            removeSpringFromSpringBody(s, scene, updateSceneTreeSelection);
    }

    public static void removeSpringsFromSceneItem(ArrayList<Spring> springs, Scene scene, boolean updateSceneTreeSelection) {
        for(Spring s : springs)
            removeSpringFromSceneItem(s, scene, updateSceneTreeSelection);
    }

    //public static void addSpringListToSpringBody(ArrayList<Spring> springList, SpringBody sb, boolean updateSceneTreeSelection) {
    public static void addSpringListToSpringBody(Collection<Spring> springList, SpringBody sb, boolean updateSceneTreeSelection) {
        sb.addSprings(springList);
        if (updateSceneTreeSelection)
            DataModelsFactory.getSceneTreeModel().fireSpringBodyChanged(sb);
    }

    static public synchronized void addSpringToEngineList(Spring spring, Scene scene) {
        scene.getJETContext().addSpring(spring);
    }

    public static synchronized void removeSpringFromEngine(Spring spring, Scene scene) {
        scene.getJETContext().removeSpring(spring);
    }

    public static void removeSpringsFromEngine(ArrayList<Spring> list, Scene scene) {
        Collection<Spring> springCol = scene.getJETContext().getSpringListSync();
        synchronized(springCol) {
            for(Spring s : list)
                springCol.remove(s);
        }
    }
}
